using Digitalmes.Application.Mes.Processes.RoutingSteps;
using Digitalmes.Application.Mes.Processes.RoutingSteps.Commands;
using Digitalmes.Application.Mes.Processes.RoutingSteps.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Processes;

/// <summary>
/// 工序步骤结点。
/// </summary>
public sealed class RoutingStepEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/process", "routingStep")
            .RequireAuthorization()
            .MapGet(GetRoutingStepListAsync, "list")
            .MapPost(CreateRoutingStepAsync)
            .MapPut(UpdateRoutingStepAsync, "{id}")
            .MapPut(UpSortRoutingStepAsync, "up/{id}")
            .MapPut(DownSortRoutingStepAsync, "down/{id}")
            .MapDelete(DeleteRoutingStepAsync, "{id}");
    }

    [EndpointDescription("查询工艺步骤集合")]
    public async Task<IApiResult> GetRoutingStepListAsync(ISender sender, [AsParameters] GetRoutingStepListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询工艺步骤")]
    public async Task<IApiResult> CreateRoutingStepAsync(ISender sender, ProcRoutingStepDto dto)
    {
        var result = await sender.Send(new CreateRoutingStepCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新工艺步骤集合")]
    public async Task<IApiResult> UpdateRoutingStepAsync(ISender sender, long id, ProcRoutingStepDto dto)
    {
        var result = await sender.Send(new UpdateRoutingStepCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("工艺步骤向上排序")]
    public async Task<IApiResult> UpSortRoutingStepAsync(ISender sender, long id)
    {
        var result = await sender.Send(new UpSortRoutingStepCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("工艺步骤向下排序")]
    public async Task<IApiResult> DownSortRoutingStepAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DownSortRoutingStepCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("删除工艺步骤")]
    public async Task<IApiResult> DeleteRoutingStepAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteRoutingStepCommand(id));
        return result.ToApiResult();
    }
}
